package com.ruoyi.teacher.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.eduadmin.api.EduadminService;
import com.ruoyi.eduadmin.api.EmploymentService;
import com.ruoyi.eduadmin.api.domain.TEmploymentTrace;
import com.ruoyi.eduadmin.api.domain.TTeachTrace;
import com.ruoyi.teacher.domain.TTeacher;
import com.ruoyi.teacher.mapper.TTeacherMapper;
import com.ruoyi.utils.SendRobotMessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teacher.mapper.TNewspaperMapper;
import com.ruoyi.teacher.domain.TNewspaper;
import com.ruoyi.teacher.service.ITNewspaperService;

/**
 * 老师日报Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-24
 */
@Service
public class TNewspaperServiceImpl implements ITNewspaperService 
{
    @Autowired
    private TNewspaperMapper tNewspaperMapper;
    @Autowired
    private EduadminService eduadminService;
    @Autowired
    private EmploymentService employmentService;
    @Autowired
    private TTeacherMapper tTeacherMapper;

    /**
     * 查询老师日报
     * 
     * @param id 老师日报主键
     * @return 老师日报
     */
    @Override
    public TNewspaper selectTNewspaperById(Long id)
    {
        return tNewspaperMapper.selectTNewspaperById(id);
    }

    /**
     * 查询老师日报列表
     * 
     * @param tNewspaper 老师日报
     * @return 老师日报
     */
    @Override
    public List<TNewspaper> selectTNewspaperList(TNewspaper tNewspaper)
    {
        return tNewspaperMapper.selectTNewspaperList(tNewspaper);
    }

    /**
     * 新增老师日报
     * 
     * @param tNewspaper 老师日报
     * @return 结果
     */
    @Override
    public AjaxResult insertTNewspaper(TNewspaper tNewspaper)
    {
        // 查询老师今日是否已填写日报
        Long userId = SecurityUtils.getUserId();
        // 查询老师今日是否已填写日报
        Boolean aBoolean = tNewspaperMapper.selectTodayNews(userId, tNewspaper.getPaperTime());
        if (aBoolean) {
            return AjaxResult.error(-1,"日报添加失败，今日已填写！");
        }
        TTeacher teacher = tTeacherMapper.findTeacherById(userId);
        String TeacherNme = teacher.getName();
        tNewspaper.setTeacherName(TeacherNme);
        tNewspaper.setTeacherId(userId);
        tNewspaper.setType(teacher.getType());
        tNewspaper.setSubject(teacher.getSubject());
        tNewspaper.setCampus(teacher.getCampus());
        // 添加教学追踪、就业追踪
//        new Thread(() -> addTrace(tNewspaper, TeacherNme)).start();
        addTrace(tNewspaper, TeacherNme);
        tNewspaper.setCreateTime(DateUtils.getNowDate());
        tNewspaperMapper.insertTNewspaper(tNewspaper);
        return AjaxResult.error(200,"日报添加成功！");
    }

    private void addTrace(TNewspaper tNewspaper, String teacherNme) {
        List<TTeachTrace> eduadminForm = tNewspaper.getEduadminForm();
        for (TTeachTrace tTeachTrace : eduadminForm) {
            tTeachTrace.setTraceTeacher(teacherNme);
            // Feign调用
            eduadminService.add(tTeachTrace);
        }
        // 添加就业追踪
        List<TEmploymentTrace> employmentForm = tNewspaper.getEmploymentForm();
        for (TEmploymentTrace tEmploymentTrace : employmentForm) {
            tEmploymentTrace.setTeacherName(teacherNme);
            employmentService.addBatch(tEmploymentTrace);
        }
    }

    /**
     * 修改老师日报
     * 
     * @param tNewspaper 老师日报
     * @return 结果
     */
    @Override
    public int updateTNewspaper(TNewspaper tNewspaper)
    {
        return tNewspaperMapper.updateTNewspaper(tNewspaper);
    }

    /**
     * 批量删除老师日报
     * 
     * @param ids 需要删除的老师日报主键
     * @return 结果
     */
    @Override
    public int deleteTNewspaperByIds(Long[] ids)
    {
        return tNewspaperMapper.deleteTNewspaperByIds(ids);
    }

    /**
     * 删除老师日报信息
     * 
     * @param id 老师日报主键
     * @return 结果
     */
    @Override
    public int deleteTNewspaperById(Long id)
    {
        return tNewspaperMapper.deleteTNewspaperById(id);
    }

    @Override
    public String batchAdd(TNewspaper newspaper) {
        return null;
    }

    @Override
    public void dailyReminder() {
        try {
            SendRobotMessageUtil.dailyReminder();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
